#ifndef CONFIG_SYS_NAND_BASE_LIST
#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
#endif

int nand_curr_device = -1;
struct mtd_info nand_infos[CONFIG_SYS_MAX_NAND_DEVICE];

static struct nand_chip nand_chips[CONFIG_SYS_MAX_NAND_DEVICE];
static u32 nand_bases[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;

static void nand_chip_init(struct mtd_info *mtd, struct nand_chip *nand, u32 base_addr)
{
	mtd->priv = nand;
	
    nand->IO_ADDR_R = nand->IO_ADDR_W = (void *)base_addr;
    if (board_nand_init(nand) == 0) {
        if (nand_scan(mtd, maxchips) == 0) {
            if (!mtd->name)
                mtd->name = (char *)default_nand_name;
            /*
             * Add MTD device so that we can reference it later
             * via the mtdcore infrastructure (e.g. ubi).
             */
            sprintf(dev_name[i], "nand%d", i);
            mtd->name = dev_name[i++];
            add_mtd_device(mtd);
        } else
            mtd->name = NULL;
    } else {
        mtd->name = NULL;
        mtd->size = 0;
	}
}

void nand_init(void)
{
    int i;
    unsigned int size = 0;
    for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
        nand_init_chip(&nand_info[i], &nand_chip[i], base_address[i]);
        size += nand_info[i].size / 1024;
        if (nand_curr_device == -1)
            nand_curr_device = i;
    }
    printf("%u MiB\n", size / 1024);
}

